<template>
  <div class="app-container">

    <div class="button" style="padding-bottom:0.5rem">
      <div class="grid-content bg-purple-dark">
        <el-row>
          <!--          {{"部门:"}}-->
          <!--          <el-select v-model="depName" placeholder="请选择部门" clearable  @change="find(currentPage=1,depName)"-->
          <!--                     style="width:15%">-->
          <!--            <el-option-->
          <!--              v-for="item in departmentList"-->
          <!--              :key="item.depName"-->
          <!--              :label="item.depName"-->
          <!--              :value="item.depName"-->
          <!--            ></el-option>-->
          <!--          </el-select>-->

          <el-button type="primary" @click="addDevice">添加设备</el-button>
        </el-row>
      </div>
    </div>

    <el-table
      element-loading-text="Loading"
      :data="deviceList"
      border
      fit
      style="width: 100%"
      highlight-current-row
    >
      <el-table-column label="设备" align="center" width="120rem">
        <template slot-scope="scope">
          <!--          {{changeAddress(scope.row.deviceImg)}}-->
          <!--          <el-image  :src="require(scope.row.address)" style="width: 100px;height:100px;display: block;margin-bottom: 20px" />-->
          <el-image :src="byteToArray(scope.row.deviceImg)"/>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center">
        <template slot-scope="scope">{{ scope.row.deviceName }}</template>
      </el-table-column>
      <el-table-column label="规格型号" align="center">
        <template slot-scope="scope">{{ scope.row.deviceSpec }}</template>
      </el-table-column>
      <el-table-column label="设备价格" align="center">
        <template slot-scope="scope">{{ scope.row.devicePrice|rounding }}</template>
      </el-table-column>
      <el-table-column label="存放位置编号" align="center">
        <template slot-scope="scope">{{ scope.row.deviceLoc }}</template>
      </el-table-column>
      <el-table-column label="设备描述" align="center" prop="deviceInfo" width="120rem" :show-overflow-tooltip="true"/>
      <el-table-column label="设备状态" align="center" width="100rem">
        <template slot-scope="scope">{{ scope.row.deviceStatus }}</template>
      </el-table-column>
      <el-table-column label="设备分类" align="center" width="100rem">
        <template slot-scope="scope">{{ scope.row.typeName }}</template>
      </el-table-column>
      <el-table-column label="是否借出" align="center" prop="isBorrow">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.isBorrow == '在库' ? 'primary' : 'warning'"
            disable-transitions>{{scope.row.isBorrow}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180rem">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="editDevice(scope.row.deviceCode)">编辑</el-button>
          <el-button type="danger" size="small" @click="delDevice(scope.row.deviceCode)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      align="center"
    ></el-pagination>
  </div>
</template>

<script>
  import $ from "@/api/deviceInfo"

  export default {
    data() {
      return {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        address: '',
        deviceList: [],
      }
    },
    filters: {
      rounding(value) {
        value = (value.toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
        return value;
      }
    },
    created() {
      // 设备列表
      this.fetchData()
    },
    methods: {
      byteToArray(path){
        let reg = new RegExp('\\\\',"g")//g,表示全部替换。
        path=path.replace(reg,"/");
        return "http://localhost:8080/tb-user/imgInfo?path="+path
      },
      changeAddress(src) {
        console.log(src.replace(/\\/g, "//"))
        this.address = "'" + src.replace(/\\/g, "//") + "'"
        // let reg=new RegExp('/','g')//g代表全部
        // let newMsg=JSON.stringify(msg).replace(reg,'&&&');

      },
      fetchData() {
        this.currentPage = 1
        $.tbDeviceList({
          pageIndex: this.currentPage,
          pageSize: this.pageSize
        }).then(res => {
          this.deviceList = res.data.list
          this.total = res.data.total;
          for (var i = 0; i < this.deviceList.length; i++) {
            this.$set(this.deviceList[i], "address", "'" + this.deviceList[i].deviceImg.replace(/\\/g, "//") + "'");

          }
          console.log(this.deviceList)
        })
      },

      addDevice() {
        this.$router.push({
          path: "add"
        });
      },
      editDevice(id) {
        this.$router.push({
          path: "edit",
          query: {id: id}
        });
      },
      //删除设备
      delDevice(id) {
        this.$confirm("此操作将删除该设备, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            $.tbDeviceDelete({deviceCode: id}).then(response => {
              if (response.success) {
                this.$message({
                  type: "success",
                  message: "删除成功"
                });
                this.fetchData();
              }
            });
            //console.log(id)
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },

      find(pageIndex) {
        $.tbDeviceList({
          pageIndex: pageIndex,
          pageSize: this.pageSize,
        }).then(res => {
          this.deviceList = res.data.list
          this.total = res.data.total;
          // for (var i = 0; i < this.administratorList.length; i++) {
          //   this.$set(this.administratorList[i], "a", i + 1);
          // }
          // console.log(res.data)
        })
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.currentPage = 1;
        if (this.state == 1) {
          this.find(this.currentPage)
        } else this.find(this.currentPage)
      },

      handleCurrentChange(val) {
        this.currentPage = val;
        //console.log(val)
        if (this.state == 1) {
          this.find(this.currentPage)
        } else this.find(this.currentPage)
      },
    }
  }
</script>
<style>
  .el-tooltip__popper {
    max-width: 700px
  }
</style>

